#!/bin/bash

# WebSocket处理器单元测试运行脚本
# 包含详细的日志记录和性能测试

echo "🧪 WebSocket处理器单元测试"
echo "================================"
echo ""

# 检查依赖
echo "📦 检查测试依赖..."
if ! command -v npm &> /dev/null; then
    echo "❌ npm 未安装"
    exit 1
fi

# 安装测试依赖
echo "📥 安装测试依赖..."
npm install --save-dev jest @types/jest ts-jest

# 创建测试目录
echo "📁 创建测试目录..."
mkdir -p test
mkdir -p coverage

# 运行测试
echo "🚀 开始运行单元测试..."
echo ""

# 运行所有测试
npm test -- --verbose --coverage --watchAll=false

# 检查测试结果
if [ $? -eq 0 ]; then
    echo ""
    echo "✅ 所有测试通过！"
    echo ""
    echo "📊 测试覆盖率报告已生成在 coverage/ 目录"
    echo "📁 HTML报告: coverage/lcov-report/index.html"
    echo "📄 JSON报告: coverage/coverage-final.json"
    echo ""
    
    # 显示覆盖率摘要
    if [ -f "coverage/coverage-summary.json" ]; then
        echo "📈 覆盖率摘要:"
        cat coverage/coverage-summary.json | jq '.total.lines.pct' 2>/dev/null || echo "   覆盖率数据已生成"
    fi
    
else
    echo ""
    echo "❌ 测试失败！"
    echo "请检查测试输出和错误信息"
    exit 1
fi

echo ""
echo "🎉 测试完成！" 